package com.dxy.app.widget.loading_view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;
import com.dxy.app.R;

/**
 * Created by dell on 2017/9/5.
 */

public class LoadingMoreView extends View {
  public LoadingMoreView(Context context) {
    super(context);
    init(null);
  }

  public LoadingMoreView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.LoadingMoreView);
    init(array);
  }

  public LoadingMoreView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.LoadingMoreView);
    init(array);
  }

  private int colorText,colorCicle;
  private float textSize;
  private Paint paintCircle;
  private Paint paintText;
  private float boaderWidth =2;
  private void init(TypedArray array){
    if(array==null){
      colorText =array.getColor(R.styleable.LoadingMoreView_more_text_color, Color.BLACK);
      colorCicle =array.getColor(R.styleable.LoadingMoreView_more_circle_color,Color.BLUE);
      textSize =array.getDimensionPixelSize(R.styleable.LoadingMoreView_more_text_size,40);
      promptContent =array.getString(R.styleable.LoadingMoreView_more_text_content);
      marginText2Circle =array.getDimensionPixelOffset(R.styleable.LoadingMoreView_more_margin_text2cicle,40);
      array.recycle();
    }else{
      colorCicle =Color.BLACK;
      colorText =Color.BLUE;
      textSize =30f;
      marginText2Circle =40;
      promptContent="正在加载中...";
    }

    paintCircle =new Paint();
    paintText =new Paint();
    initPaint(paintCircle,paintText);

    paintText.setTextSize(textSize);
    paintText.setColor(colorText);
    paintCircle.setColor(colorCicle);
    paintCircle.setStyle(Paint.Style.STROKE);
    paintCircle.setStrokeWidth(boaderWidth*2);
    postDelayed(()->startRotate(),2000);
  }

  private float width,height;//控件宽高
  private float offsetCenter =10;//绘制的起始位置与中心位置的偏移量
  private float startPromptX,startPromptY;//提示文字的宽高
  @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    width =w;
    height =h;
    initParams();
  }
  private String promptContent;//提示内容
  private void initParams(){
    float promptWidth =paintText.measureText(promptContent);

    circleRadius =height/8;//默认圆圈的直径为控件的1/4

    drawRegionWidth = promptWidth+marginText2Circle+circleRadius*2;//绘制区域的宽度等于绘制字体宽度+字体圆圈之间的间距+圆圈的最大半径
    float startX =(width-drawRegionWidth)/2;

    float startY =height/2;
    maxOffsetXLength =circleRadius/2;

    offsetLeftX =startX+circleRadius-offsetCenter;
    offsetRightX =startX+circleRadius+offsetCenter;

    startPromptX =startX +2*curRadius+marginText2Circle*2;
    startPromptY =startY+paintText.descent();

    arcRegion =new RectF(startX,startY-circleRadius,startX+circleRadius*2,startY+circleRadius);
  }
  private RectF arcRegion;//绘制圆弧的区域
  private float drawRegionWidth;//绘制区域的宽度
  private float marginText2Circle;//绘制文字和圆圈之间的间距
  private void initPaint(Paint...paints){
    for(Paint each:paints){
      each.setAntiAlias(true);
      each.setStrokeCap(Paint.Cap.ROUND);
    }
  }

  private float circleRadius;//circle的半径
  private int startAngle;//起始角度

  private int curRadius=0;//当前半径
  public static final String TAG="TAG_LoadingMoreView";
  private float offsetLeftX,offsetRightX;
  private float curOffset =0;
  private float maxOffsetXLength;//最大的水平宽度
  @Override protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if(curRadius!=0){
      RectF rect =new RectF(arcRegion.left+curRadius,arcRegion.top+curRadius,arcRegion.right-curRadius,arcRegion.bottom-curRadius);
      canvas.drawArc(rect,startAngle,90,false,paintCircle);
      canvas.drawArc(rect,startAngle+180,90,false,paintCircle);
    }else{
      float delOffsetX =0;
      if(curOffset>maxOffsetXLength){
        delOffsetX =curOffset-maxOffsetXLength;
      }
      RectF rLeft =new RectF(offsetLeftX-curOffset,height/2-boaderWidth/2,offsetLeftX-delOffsetX,height/2+boaderWidth/2);
      canvas.drawRoundRect(rLeft,boaderWidth,boaderWidth,paintCircle);
      RectF rRight =new RectF(offsetRightX+delOffsetX,height/2-boaderWidth/2,offsetRightX+curOffset,height/2+boaderWidth/2);
      canvas.drawRoundRect(rRight,boaderWidth,boaderWidth,paintCircle);
    }
    canvas.drawText(promptContent,startPromptX,startPromptY,paintText);
  }
  private void calculateValues(float value){
    if(value>=1){
      value-=1;
      value =value/2;
      startAngle = (int) (360*value);
      curRadius = (int) (circleRadius*value);
    }else{
      curRadius =0;//将当前curRadius置为0
      curOffset =circleRadius*value;
    }
    invalidate();
  }
  ValueAnimator animator;
  private void startRotate(){
    if(animator==null){
      animator =new ValueAnimator();
      animator.setFloatValues(0,1f,2f,3f);
      animator.setRepeatCount(-1);
      animator.setInterpolator(new LinearInterpolator());
      animator.setDuration(1500);
      animator.addUpdateListener(animation->{
        float value = (float) animation.getAnimatedValue();
        calculateValues(value);
      });
    }
    animator.start();
  }
}
